﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Build.Workflow.Activities;
using Microsoft.TeamFoundation.Lab.Workflow.Activities;

namespace HybridLab.Workflow.Activities.UI
{
	internal static class DeploymentScriptUtils
	{
		private static readonly string format = "{0} | {1}";

		public static List<DeploymentScript> ToDeploymentScript(StringList stringList)
		{
			List<DeploymentScript> list = new List<DeploymentScript>();
			if (stringList != null)
			{
				foreach (string str in stringList)
				{
					string[] strArray = str.Split(new char[] { '|' });
					if (strArray.Length > 0)
					{
						DeploymentScript item = new DeploymentScript();
						item.Machine = strArray[0].Trim();
						if (strArray.Length > 1)
						{
							item.Script = strArray[1].Trim();
							if (strArray.Length > 2)
							{
								item.WorkingDirectory = strArray[2].Trim();
							}
						}
						list.Add(item);
					}
				}
			}
			return list;
		}

		public static StringList ToStringList(List<DeploymentScript> scripts)
		{
			StringList list = new StringList();
			if (scripts != null)
			{
				foreach (DeploymentScript script in scripts)
				{
					string item = string.Format(format, script.Machine, (script.Script != null) ? script.Script.Trim() : string.Empty);
					if (!string.IsNullOrEmpty(script.WorkingDirectory))
					{
						item = string.Format(format, item, script.WorkingDirectory.Trim());
					}
					list.Add(item);
				}
			}
			return list;
		}
	}
}
